package pruebas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;
import negModel.AlgoPesos;
import negModel.Tanque;
import negModel.Combustible;
import negModelExcep.*;

public class TanqueTest extends TestCase {

	Combustible comb;
	Tanque tanque,tanque2;
	Element e;
	
	protected void setUp() throws Exception {
		super.setUp();
		
		comb = new Combustible(88,1);
		tanque = new Tanque(100,50,new String ("tanque"),new AlgoPesos(100),true,100,100,comb);
		tanque.cargarComb();

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
	    e = doc.createElement("parte");
		e.setAttribute("Clase","tanque");
		
		e.setAttribute("maxVU", ""+ tanque.getMaxVU());
		e.setAttribute("vidaUtil", ""+ tanque.getVidaUtil());
		e.setAttribute("peso", ""+ (tanque.getPeso()-comb.pesar()));
		e.setAttribute("aportevmax", ""+ tanque.aportevmax());
		e.setAttribute("detalle", tanque.getDetalle());
		e.setAttribute("precio", ""+(tanque.getPrecio()).getMonto());
		e.setAttribute("removible", ""+tanque.esRemovible());
		e.setAttribute("performance", ""+tanque.getPerformance());
		
		e.setAttribute("capacidad", ""+tanque.getCapacidad());
		e.appendChild(comb.toXml(doc));
		
	}

	public void testGetPeso() {
		assertEquals(150,tanque.getPeso(),0.1);	
	}

	public void testGastar() {
		try {
			tanque.gastar();
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		assertEquals(99,tanque.getVidaUtil(),0.1);	
	}

	public void testFuncionar() {
		try {
			assertEquals(88,tanque.funcionar(),0.1);
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}	
	}

	public void testGetCapacidad() {
		assertEquals(100,tanque.getCapacidad(),0.1);
	}

	public void testGetCantComb() {
		assertEquals(100,tanque.getCantComb(),0.1);
	}
	
	public void testToXml () throws Exception {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=tanque.toXml(doc);
		
		assertEquals(tanque.getCapacidad(),Double.parseDouble (e.getAttribute("capacidad")),0.0);
		NodeList  nodosComb=e.getElementsByTagName("combustible");
		for(int nc=0;nc<nodosComb.getLength();nc++){
			assertEquals(comb.getCantidad(),Double.parseDouble(((Element)nodosComb.item(nc)).getAttribute("cantidad")),0.0);
			assertEquals(comb.getPesoxL(),Double.parseDouble(((Element)nodosComb.item(nc)).getAttribute("pesoxL")),0.0);
			assertEquals(comb.getOctanos(),Float.parseFloat(((Element)nodosComb.item(nc)).getAttribute("octanos")),0.0);
			}
		
		assertEquals(tanque.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((tanque.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(tanque.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(tanque.getDetalle(),e.getAttribute("detalle"));
		assertEquals((tanque.getPeso()),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(tanque.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}
	
	public void testTanque()throws Exception {
		
		tanque2=new Tanque(e);
				
		assertEquals(tanque2.getCapacidad(),Double.parseDouble (e.getAttribute("capacidad")),0.0);
		NodeList  nodosComb=e.getElementsByTagName("combustible");
		for(int nc=0;nc<nodosComb.getLength();nc++){
			assertEquals(comb.getCantidad(),Double.parseDouble(((Element)nodosComb.item(nc)).getAttribute("cantidad")),0.0);
			assertEquals(comb.getPesoxL(),Double.parseDouble(((Element)nodosComb.item(nc)).getAttribute("pesoxL")),0.0);
			assertEquals(comb.getOctanos(),Float.parseFloat(((Element)nodosComb.item(nc)).getAttribute("octanos")),0.0);
			}
		assertEquals(tanque2.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((tanque2.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(tanque2.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(tanque2.getDetalle(),e.getAttribute("detalle"));
		assertEquals((tanque2.getPeso()-comb.pesar()),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(tanque2.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}

}
